//
//  HPOriganalView.swift
//  MyWeibo
//
//  Created by Apeng on 16/4/14.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let homePageCellMargin: CGFloat = 8

class HPOriganalView: UIView {
    
    var bottomCons: Constraint?
    
    // 拥有模型, 绑定数据
    var statusCellVM: HPStatusCellViewModel? {
        
        didSet {
            
         // 设置用户头像
            iconImageView.sd_setImageWithURL(statusCellVM?.headImgURL)
            // 昵称
            nameLabel.text = statusCellVM?.status?.user?.name
            // 等级
            mbRankImageView.image = statusCellVM?.mbImg
            // 认证
            verifiedImageView.image = statusCellVM?.verified_typeImg
            
            // 设置文本的内容
            contantLabel.text = statusCellVM?.status?.text
            // 时间
            timeLabel.text = statusCellVM?.created_at
            // 来源
            sourceLabel.text = statusCellVM?.source
            
            // 配图数据源 (属于原创微博部分)
            statusPictureView.imageURLs = statusCellVM?.status?.pic_urls
            
            // 防止cell 的复用,  先 卸载 约束  在更新 约束if else 判断是否要显示
            self.bottomCons?.uninstall()
            
            if let url = statusCellVM?.status?.pic_urls where url.count > 0 {
                
                // 说明有图片, 需要更新原创微博的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(statusPictureView.snp_bottom).offset(homePageCellMargin).constraint
                })
                
                // 隐藏
                statusPictureView.hidden = false
                
            } else {
                // 没有配图, 也要更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contantLabel.snp_bottom).offset(homePageCellMargin).constraint
                })
                
                // 设置数据源为nil
                statusPictureView.imageURLs = nil
                
                // 隐藏配图视图
                statusPictureView.hidden = true
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 背景颜色
        backgroundColor = UIColor.whiteColor()
        
        // 圆角
        iconImageView.cornerRadious = 20
        // 约束
        addSubview(headView)
        addSubview(iconImageView)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contantLabel)
        // 添加到界面
        addSubview(statusPictureView)
        
        // 添加headView
        headView.snp_makeConstraints { (make) -> Void in
            make.top.right.left.equalTo(self)
            make.height.equalTo(homePageCellMargin)
        }
        
        // 设置约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headView.snp_bottom).offset(homePageCellMargin)
            make.left.equalTo(self.snp_left).offset(homePageCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_top)
            make.left.equalTo(iconImageView.snp_right).offset(homePageCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(homePageCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.right.equalTo(iconImageView)
            
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(nameLabel.snp_left)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(homePageCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        contantLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_left)
            make.top.equalTo(iconImageView.snp_bottom).offset(homePageCellMargin)
        }
        
        
        
        // 微博配图
        statusPictureView.snp_makeConstraints { (make) -> Void in
//            make.right.equalTo(contantLabel.snp_right)
            make.left.equalTo(contantLabel.snp_left)
            make.top.equalTo(contantLabel.snp_bottom).offset(homePageCellMargin)
            // 设置图片的大小尺寸
            make.size.equalTo(CGSize(width: 100, height: 150))
        }
        
        
        // 设置原创微博算行高
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contantLabel.snp_bottom).offset(homePageCellMargin)
            // 修改高度
           self.bottomCons = make.bottom.equalTo(statusPictureView.snp_bottom).offset(homePageCellMargin).constraint
        }
    }
    

// 懒加载控件
    // 头像
    private lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 等级
    private lazy var mbRankImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 认证
    private lazy var verifiedImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 昵称
    private lazy var nameLabel = UILabel(text: "name", textColor: UIColor.darkGrayColor(), fontSzie: 15)
    // 时间
    private lazy var timeLabel = UILabel(text: "time", textColor: UIColor.orangeColor(), fontSzie: 15)
    // 来源
    private lazy var sourceLabel = UILabel(text: "sourceLabel", textColor: UIColor.darkGrayColor(), fontSzie: 15)
    // 微博正文
    private lazy var contantLabel = UILabel(text: "contantLabel", textColor: UIColor.darkGrayColor(), fontSzie: 15, margin: homePageCellMargin)
    
    // 微博分割线
    private lazy var headView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.yellowColor()
        return v
    }()
    
    // 微博配图
    private lazy var statusPictureView: HPStatusPictureView = HPStatusPictureView()
}

